﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace UpLoad
{
    public abstract class UpLoadBase
    {
        /// <summary>
        /// 获取生成的文件名称
        /// </summary>
        /// <param name="filename">上传前的文件完整名称</param>
        /// <returns></returns>
        public virtual string GetOnlyName(string suffix)
        {
            string newname = Guid.NewGuid() + suffix;   //通过GUID来取名字
            return newname;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public virtual bool DeleteRecord(string filename)
        {
            string filepath=Config.DefaultServerPath;
            return RemoveFile(filepath, filename);
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public virtual bool RemoveFile(string filepath, string filename)
        {
            FileDal dal = new FileDal();
            if (dal.Delete(filename))
            {
                string fullname = Path.Combine(filepath, filename);
                //删除服务器上的文件
                try
                {
                    File.Delete(fullname);
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取扩展名称 2012-5-14 李学明
        /// </summary>
        /// <param name="filename">上传文件名称</param>
        /// <returns>扩展名称</returns>
        public virtual string GetSuffix(string filename)
        {

            return !string.IsNullOrEmpty(filename) ? Path.GetExtension(filename).ToLower() : "";
            // return filename.Substring(filename.LastIndexOf("."), filename.Length - (filename.LastIndexOf("."))).ToLower();       //获取后缀名
        }

        public virtual string GetFileType(string suffix)
        {
            if(Config.imagesuffixs.Contains(suffix.Trim()))
                return "图片";
            else if(Config.videosuffixs.Contains(suffix.Trim()))
                return "视频";
            else if(Config.musicsuffixs.Contains(suffix.Trim()))
                return "音频";
            else
                return "其他文件";
        }

        public virtual ReturnModel SaveFile(FileUpload uploadfile)
        {
            ReturnModel model = new ReturnModel();
            string suffix=GetSuffix(uploadfile.FileName);
            string filename= GetOnlyName(suffix);
            string fullname = Path.Combine(Config.DefaultPath, filename);
            if (UpLoad(uploadfile, fullname))
            {
                FileInfoModel info = new FileInfoModel();
                info.filename = filename;
                info.filetype = GetFileType(suffix);
                info.createtime = DateTime.Now;
                info.updatetime = DateTime.Now;
                model.FileInfo = info;
                if (new FileDal().AddFile(model.FileInfo))
                    model.IsSuccess = true;
            }
            else
            {
                model.IsSuccess = false;
            }
            return model;
        }

        /// <summary>
        /// 真正上传 这里要抛出异常，如果出错，很难处理的 2012-5-14 李学明
        /// </summary>
        /// <param name="uploadfile">上传控件</param>
        /// <param name="uploadPath">上传路径</param>
        /// <returns></returns>
        public virtual bool UpLoad(FileUpload uploadfile,string fullname)
        {
            if (!uploadfile.HasFile)
            {
                return false;
            }
            try
            {
                uploadfile.PostedFile.SaveAs(fullname);
            }
            catch(Exception)
            {
                throw;
               // return false;
            }
            return true;
        }


        public virtual string GetServerPath(string filename)
        {
            string filepath = Config.DefaultServerPath;
            return Path.Combine(filepath, filename).ToString();
        }


        //修改数据
        public virtual ReturnModel UpdateFile(FileUpload fileupload, string filename)
        {
            return ModifyFile(fileupload, filename, Config.DefaultPath, Config.DefaultServerPath);
        }

        //真正修改
        public virtual ReturnModel ModifyFile(FileUpload fileupload, string filename, string uploadpath, string modifypath)
        {
            ReturnModel model=new ReturnModel();
            if (fileupload.HasFile)
            {
                try
                {
                    File.Delete(Path.Combine(modifypath, filename));
                    model = SaveFile(fileupload);
                    if (!new FileDal().Delete(filename))
                        model.IsSuccess = false;
                }
                catch (Exception)
                {
                    model.IsSuccess = false;
                }
            }
            else
            {
                try 
	            {
                    File.Delete(Path.Combine(modifypath, filename));
                    model.IsSuccess = true;
	            }
	            catch (Exception)
	            {
	                throw;
                   // model.IsSuccess = false;
                } 
            }
            return model;
        }
    }
}